﻿using Cmp.Web.Common;
using Cmp.Web.IRepository;
using Cmp.Web.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace Cmp.Web.Repository
{
    public class PatientProvide : DbContext<PatientInfo>, IPatientProvide
    {
        /// <summary>
        /// 根据卡信息获取患者信息
        /// </summary>
        /// <param name="CardType"></param>
        /// <param name="CardNum"></param>
        /// <param name="exception"></param>
        /// <returns></returns>
        public Task<PatientInfo> GetPatientInfoByCard(CardType CardType, string CardNum, out ExceptionAll exception)
        {
            exception = new ExceptionAll();
            try
            {
                return Task.FromResult(Db.Queryable<PatientInfo>().Where(
                       x =>
                       (CardType == CardType.MedicareCard ? x.PatientMedCard == CardNum :
                       CardType == CardType.VisitingCard ? x.PatientVistCard == CardNum :
                       CardType == CardType.IdCard ? x.PatientIdCard == CardNum :
                       CardType == CardType.HealthCard ? x.PatientHealCard == CardNum : false)).First());
            }
            catch (Exception ex)
            {
                exception.errMsg = ex.Message;
                exception.Action = System.Reflection.MethodBase.GetCurrentMethod()!.Name;
                exception.errMsgId = ErrorType.MyInterfaceError;
                exception.Exception = ex;
            }
            return null;
        }
        /// <summary>
        /// 根据患者id获取患者信息
        /// </summary>
        /// <param name="patientId"></param>
        /// <param name="exception"></param>
        /// <returns></returns>
        public Task<PatientInfo> GetPatientInfoByPatientId(string patientId, out ExceptionAll exception)
        {
            exception = new ExceptionAll();
            try
            {
                return Task.FromResult(Db.Queryable<PatientInfo>().Where(x => x.PatientId == patientId).First());
            }
            catch (Exception ex)
            {
                exception.errMsg = ex.Message;
                exception.Action = System.Reflection.MethodBase.GetCurrentMethod()!.Name;
                exception.errMsgId = ErrorType.MyInterfaceError;
                exception.Exception = ex;
            }
            return null;
        }
        /// <summary>
        /// 患者建档
        /// </summary>
        /// <param name="patient"></param>
        /// <param name="exception"></param>
        /// <returns></returns>
        public Task<bool> PatientDocumentation(PatientInfo patient, out ExceptionAll exception)
        {
            exception = new ExceptionAll();
            var result = false;
            try
            {
                result = Db.Insertable<PatientInfo>(patient).ExecuteCommand() > 0;
            }
            catch (Exception ex)
            {
                exception.errMsg = ex.Message;
                exception.Action = System.Reflection.MethodBase.GetCurrentMethod()!.Name;
                exception.errMsgId = ErrorType.MyInterfaceError;
                exception.Exception = ex;
            }
            return Task.FromResult(result) ;
        }
    }
}
